package com.example.yoho.mvp.presenter;

import com.example.lib_core.base.BaseEntity;
import com.example.lib_core.mvp.presenter.BasePresenter;
import com.example.yoho.mvp.contract.StrlloContract;
import com.example.yoho.mvp.model.entity.StrlloListEntity;
import com.example.yoho.mvp.model.entity.StrlloSortEntity;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class StrlloPresenter extends BasePresenter<StrlloContract.Model,StrlloContract.View> {
    private int menuid=1;
    private int categoryid=1;
    private int page=0;


    @Inject
    public StrlloPresenter() {
    }

    public void reuqestStrllo(){
        this.menuid=menuid;
        this.categoryid=categoryid;
        page=0;

        JSONObject strlloSortJob = new JSONObject();
        JSONObject strlloListJob = new JSONObject();


        try {
            strlloSortJob.put("menuid","1");
            strlloListJob.put("categoryid","1");
            strlloListJob.put("page",""+page);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Observable<BaseEntity> ob = Observable.merge(
                mModel.strlloSortModel(strlloSortJob.toString()),
                mModel.strlloListModel(strlloListJob.toString()));

        ob.observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposables.add(d);
                        mView.showDialog();
                    }

                    @Override
                    public void onNext(BaseEntity baseEntity) {
                        if (baseEntity instanceof StrlloSortEntity){
                             StrlloSortEntity strlloSortEntity= (StrlloSortEntity) baseEntity;
                             mView.showStrlloSort(strlloSortEntity);
                        }else if (baseEntity instanceof StrlloListEntity){
                            StrlloListEntity strlloListEntity= (StrlloListEntity) baseEntity;
                            mView.showStrolloList(strlloListEntity);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showToast(""+e.getMessage());
                        mView.hideDialog();
                    }

                    @Override
                    public void onComplete() {
                        mView.showDialog();
                    }
                });
    }
}
